<script setup>
import { ref } from 'vue'
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";
import axios from "axios";
const form = ref({
  username: '',
  password: '',
  repassword: ''
})
const router = useRouter()

const onSubmit = () => {

  if (form.value.password !== form.value.repassword) {
    ElMessage.error('注册失败，两次密码不一致')
    form.value.password = ''
    form.value.repassword = ''
    return
  }

  const formData = {
    username: form.value.username,
    password: form.value.password
  }
  const url = '/user/register'
  axios.post(url,
    JSON.stringify(formData)
  ).then((res)=>{
    if (res.data.flag) {
      ElMessage.success(res.data.data)
      router.push("/login")
    } else {
      ElMessage.error(res.data.message)
    }
  })
}
</script>

<template>
  <el-form class="form" style="padding: 40px 50px 50px;" :model="form">
    <h1 style="font-size: 35px;font-weight: 600;line-height: 30px;margin-bottom: 25px;">
      欢迎注册FY音乐
    </h1>
    <h2 style="font-size: 19px;margin-bottom: 32px">
      畅游音乐世界
    </h2>
    <el-form-item>
      <el-input class="input" maxlength="15" placeholder="用户名" style="width: 370px;height: 47px" v-model="form.username" />
    </el-form-item>
    <el-form-item>
      <el-input class="input"  maxlength="15" placeholder="密码" style="width: 370px;height: 47px" v-model="form.password" type="password" />
    </el-form-item>
    <el-form-item>
      <el-input class="input" placeholder="确认密码" style="width: 370px;height: 47px" v-model="form.repassword" type="password" />
    </el-form-item>
    <el-form-item>
      <el-button class="button" type="danger" @click="onSubmit">注 册</el-button>
    </el-form-item>
  </el-form>
</template>

<style scoped>
* {
  font-family: '微软雅黑 Light';
  font-weight: 600;
  color: #333;

}
.input {
  opacity: 0.7;
}
/*.form {
  width: 340px;
  background: white;
  position: absolute;
  left: calc(50vw - 220px);
  top: 25%
}*/
.form {
  width: 340px;
  position: absolute;
  left: calc(50vw - 220px);
  top: 25%
}

.button {
  background: linear-gradient(to right bottom,#e96161,#e9b38b);
  width: 100%;
  height: 44px;
  color: white;
  cursor: pointer;
  text-align: center;
  font-size: 18px;
  line-height: 44px;
  outline: none;
}
</style>
